package com.piotrs10.bsk.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.piotrs10.bsk.model.Klient;
import com.piotrs10.bsk.model.Samochod;
import com.piotrs10.bsk.model.TypKlienta;

public class KlientDao extends BaseDao {
	public Klient get(int id) {
		Connection connection = this.getConnection();
		PreparedStatement statement = null;
		Klient klient = null;
		try {
			statement = connection.prepareStatement("SELECT imie, nazwisko, adres," +
					" typ_id FROM klient WHERE id = ?;");
			statement.setInt(1, id);
			ResultSet set  = statement.executeQuery();
			set.next();
			klient = new Klient(id, set.getString(1), set.getString(2), set.getString(3),
					set.getInt(4));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return klient;
	}
	
	public void delete(Integer id) {
		Connection connection = this.getConnection();
		PreparedStatement statement = null;
		try {
			statement = connection.prepareStatement("DELETE FROM Klient " +
						"WHERE id = ?");
			statement.setInt(1, id);
			statement.executeUpdate();
			System.out.println(statement.toString());
			connection.commit();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			if(connection != null)
				try {
					connection.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		} finally {
			if(statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	public ArrayList<Klient> getAll() {
		Connection connection = this.getConnection();
		PreparedStatement statement = null;
		ArrayList<Klient> list = null;
		try {
			statement = connection.prepareStatement("SELECT id, imie, nazwisko, adres, typ_id" +
					" FROM klient;");
			ResultSet set = statement.executeQuery();
			list = new ArrayList<Klient>();
			while(set.next()) {
				list.add(new Klient(set.getInt(1), set.getString(2), set.getString(3), set.getString(4),
						set.getInt(5)));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return list;
	}
	
	public void save(boolean insert, Klient klient) {
		Connection connection = this.getConnection();
		PreparedStatement statement = null;
		try {
			if(insert) {
				statement = connection.prepareStatement("INSERT INTO Klient " +
						"(imie, nazwisko, adres, typ_id) VALUES " +
						"(?, ?, ?, ?);");
			} else {
				statement = connection.prepareStatement(
						"UPDATE Klient SET imie = ? , nazwisko = ?, adres = ?, typ_id = ? " +
						"WHERE id = ?;");
			}
			statement.setString(1, klient.getImie());
			statement.setString(2, klient.getNazwisko());
			statement.setString(3, klient.getAdres());
			statement.setInt(4, klient.getTyp());
			if(!insert)
				statement.setInt(5, klient.getId());
			
			statement.executeUpdate();
			System.out.println(statement.toString());
			connection.commit();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
